import { v4 as uuidv4 } from 'uuid'
import { readonly, ref } from 'vue'
import { useLocalStorage } from '@vueuse/core'

const OAUTH_ID_KEY = '__OAUTH_ID'
const isPwa = window.matchMedia('(display-mode: standalone)').matches

export function useOauth() {
  const oauthId = useLocalStorage(OAUTH_ID_KEY, '')
  if (oauthId.value === '') {
    oauthId.value = uuidv4()
  }
  const oauthType = readonly(ref(isPwa ? 'pwa' : 'web'))
  const version = readonly(ref('1.1'))

  return {
    oauthId,
    oauthType,
    version,
  }
}
